// pages/album/rewardLog.js
import request from '../../utils/request'
import Datetime from '../../utils/datetime'
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: 0,
    rewardData: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id: options.id
    })
    this.initData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /*
   * 初始化打赏列表数据
   */
  initData(callback) {
    let that = this
    let url = app.globalData.apiHost + '/albums/' + this.data.id + '/reward_log/'
    request(url, 'GET', app.globalData.loginToken, {}, function (err, data) {
      console.log(data)
      if (!err) {
        that._setDatetime(data)
        that.setData({ rewardData: data })
      }
      if (callback) {
        return callback()
      }
    })
  },


  /*
   * 加载更多数据
   */
  loadMore() {
    let that = this
    let rewardData = this.data.rewardData
    if (rewardData.next) {
      request(rewardData.next, 'GET', app.globalData.loginToken, {}, function (err, data) {
        if (!err) {
          that._setDatetime(data)
          rewardData.next = data.next
          rewardData.previous = data.previous
          rewardData.results = rewardData.results.concat(data.results)
          that.setData({ rewardData: rewardData })
        }
      })
    }
  },

  /*
   * 操作时间
   */
  _setDatetime(data) {
    for (let i = 0; i < data.results.length; i++) {
      let item = data.results[i]
      let datetime = new Datetime(item.created)
      item.createTime = datetime.getDateStr()
    }
  },

})